{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### CUDA语义的支持"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Default Device:cpu\n",
      "Device Type:cpu\n",
      "CPU Device:【cpu:0】\n",
      "GPU Device:【cuda:0】\n",
      "GPU Device:【cuda:0】\n",
      "Total GPU Count:1\n",
      "Total CPU Count:12\n",
      "GeForce GTX 1060\n",
      "GPU Is Available:True\n"
     ]
    }
   ],
   "source": [
    "#使用torch.device指定设备,Pytorch默认使用cpu设备\n",
    "print(\"Default Device:{}\".format(torch.Tensor([4,5,6]).device))\n",
    "#cpu设备可以使用\"cpu:0\"来指定\n",
    "device = torch.Tensor([1,2,3],device=\"cpu:0\").device\n",
    "print(\"Device Type:{}\".format(device))\n",
    "#用torch.device指定cpu:0设备\n",
    "cpu1 =torch.device(\"cpu:0\")\n",
    "print(\"CPU Device:【{}:{}】\".format(cpu1.type,cpu1.index))\n",
    "#使用索引的方式，默认设备为CUDA设备\n",
    "gpu =torch.device(0)\n",
    "print(\"GPU Device:【{}:{}】\".format(gpu.type,gpu.index))\n",
    "#也可以通过torch.device(\"cuda:0\")的方式指定使用哪一块GPU\n",
    "gpu =torch.device(\"cuda:0\")\n",
    "print(\"GPU Device:【{}:{}】\".format(gpu.type,gpu.index))\n",
    "#查看所有可用GPU个数\n",
    "print(\"Total GPU Count:{}\".format(torch.cuda.device_count()))\n",
    "#获取系统CPU数量\n",
    "print(\"Total CPU Count:{}\".format(torch.cuda.os.cpu_count()))\n",
    "#获取GPU设备名称\n",
    "print(torch.cuda.get_device_name(torch.device(\"cuda:0\")))\n",
    "#GPU设备是否可用\n",
    "print(\"GPU Is Available:{}\".format(torch.cuda.is_available()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 将Tensor从CPU转移到GPU并进行计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cpu\n",
      "cuda:0\n",
      "cuda:0\n",
      "cuda:0\n",
      "tensor([[  1.,  64., 343.],\n",
      "        [ 27., 216., 729.],\n",
      "        [  8., 125., 512.]])\n"
     ]
    }
   ],
   "source": [
    "cpu_tensor = torch.Tensor([[1,4,7],[3,6,9],[2,5,8]])\n",
    "print(cpu_tensor.device)\n",
    "#使用to()方法将cup_tensor转到GPU上\n",
    "gpu_tensor1 = cpu_tensor.to(torch.device(\"cuda:0\"))\n",
    "print(gpu_tensor1.device)\n",
    "#使用cuda()方法将cpu_tensor转移到GPU上\n",
    "gpu_tensor2 = cpu_tensor.cuda(torch.device(\"cuda:0\"))\n",
    "print(gpu_tensor2.device)\n",
    "#使用copy_()方法将cpu_tensor转移到GPU上\n",
    "gpu_tensor3 = cpu_tensor.copy_(gpu_tensor2)\n",
    "print(gpu_tensor2.device)\n",
    "print(gpu_tensor3**3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 直接在GPU上创建Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cuda:0\n",
      "cpu\n",
      "tensor([[0.8562, 0.4634, 0.5491, 0.5583],\n",
      "        [0.6402, 0.0205, 0.4074, 0.0257],\n",
      "        [0.8147, 0.7708, 0.5986, 0.5964]], device='cuda:0')\n",
      "tensor([[0., 0., 0., 0., 0.],\n",
      "        [0., 0., 0., 0., 0.]], device='cuda:0')\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#使用torch.tensor方法，注意和torch.Tensor的区别,前者可通过device指定GPU设备，而后者只能在CPU上创建。\n",
    "gpu_tensor1 = torch.tensor([[2,5,8],[1,4,7],[3,6,9]],device=torch.device(\"cuda:0\"))\n",
    "print(gpu_tensor1.device)\n",
    "#Tensor方法device只能是CPU设备，否则报错。在CPU上创建后可使用copy_()、cuda()、to()等方法转移到GPU上\n",
    "cpu_tensor = torch.Tensor([[2,5,8],[1,4,7],[3,6,9]],device=torch.device(\"cpu:0\"))\n",
    "print(cpu_tensor.device)\n",
    "#除了Tensor方法外，torch上的其它例如rand、randn等方法都可以通过device参数指定GPU设备\n",
    "print(torch.rand((3,4),device=torch.device(\"cuda:0\")))\n",
    "print(torch.zeros((2,5),device=torch.device(\"cuda:0\")))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### CUDA Stream"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x0>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0daf0>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0daf0>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0daf0>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0daf0>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0daf0>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0daf0>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0daf0>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0daf0>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0daf0>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0daf0>\n"
     ]
    }
   ],
   "source": [
    "cuda = torch.device('cuda')\n",
    "#创建新的Stream\n",
    "s = torch.cuda.Stream()\n",
    "#使用默认Stream\n",
    "A = torch.randn((1, 10), device=cuda)\n",
    "print(\"Current Stream:{}\".format(torch.cuda.current_stream()))\n",
    "for i in range(10):\n",
    "    #在新的Stream上对默认Stream上创建的Tenssor求和\n",
    "    with torch.cuda.stream(s):\n",
    "        print(\"Current Stream:{}\".format(torch.cuda.current_stream()))\n",
    "        # sum() may start execution before randn() finishes!\n",
    "        B = torch.sum(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Default Stream:<torch.cuda.Stream device=0 cuda_stream=0x0>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0dd30>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0dd30>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0dd30>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0dd30>\n",
      "Current Stream:<torch.cuda.Stream device=0 cuda_stream=0x1eae6e0dd30>\n"
     ]
    }
   ],
   "source": [
    "cuda = torch.device('cuda')\n",
    "#创建新的Stream\n",
    "s = torch.cuda.Stream()\n",
    "#使用默认Stream\n",
    "A = torch.randn((100000, 7000), device=cuda)\n",
    "default_stream = torch.cuda.current_stream()\n",
    "print(\"Default Stream:{}\".format(default_stream))\n",
    "#等待创建A的stream完成\n",
    "torch.cuda.Stream.synchronize(default_stream.cuda_stream)\n",
    "#在新的Stream上对默认Stream上创建的Tenssor求和\n",
    "with torch.cuda.stream(s):\n",
    "    for i in range(5):\n",
    "        print(\"Current Stream:{}\".format(torch.cuda.current_stream()))\n",
    "        B = torch.sum(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#释放没有使用的缓存数据\n",
    "torch.cuda.empty_cache()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2802188288"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取缓存数据大小\n",
    "torch.cuda.memory_cached()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2803236864"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#最大缓存大小\n",
    "torch.cuda.max_memory_cached()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2801129984"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#最大分配内存大小\n",
    "torch.cuda.max_memory_allocated()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 固定内存缓冲区"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id:2858737070368\n",
      "id:2858735080432\n",
      "id:2858737070440\n",
      "tensor([[ 1.,  2.,  4.],\n",
      "        [ 5.,  7.,  9.],\n",
      "        [ 3.,  7., 10.]], device='cuda:0')\n",
      "is_pinned:False/True\n"
     ]
    }
   ],
   "source": [
    "# pytorch中提出了固定缓冲区的概念，当从CPU设备的固定缓冲区(页面锁定)拷贝数据到GPU时，速度会有很大提升。\n",
    "# 普通tensor上调用pin_memory方法即将tensor放入固定缓冲区中\n",
    "x = torch.Tensor([[1,2,4],[5,7,9],[3,7,10]])\n",
    "y = torch.pin_memory(x)\n",
    "#或者x直接调用pin_memory方法\n",
    "z = x.pin_memory()\n",
    "#需要注意：pin_memory方法对应的拷贝，因此内存地址是不一样的\n",
    "print(\"id:{}\".format(id(x)))\n",
    "print(\"id:{}\".format(id(y)))\n",
    "print(\"id:{}\".format(id(z)))\n",
    "#一旦将tensor放入固定缓冲区，可以使用asynchrinize方式拷贝数据到GPU，例如使用cuda()方法，将关键字参数non_blocking设置为True\n",
    "a = z.cuda(non_blocking=True)\n",
    "print(a)\n",
    "print(\"is_pinned:{}/{}\".format(x.is_pinned(),z.is_pinned()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#在数据加载器Dataloader中指定pin_memory关键字参数为true"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "#从视觉库中导入数据集及图片数据转化方法\n",
    "from torchvision import datasets, transforms\n",
    "#穿件Dataset实例\n",
    "mnist_dataset = datasets.MNIST('../data', train=True, download=True,\n",
    "                    transform=transforms.Compose([\n",
    "                        transforms.ToTensor(),\n",
    "                        transforms.Normalize((0.1,), (0.3,))\n",
    "                    ]))\n",
    "#创建DataLoader,加载MNIST数据集\n",
    "data_loader = torch.utils.data.DataLoader(mnist_dataset,batch_size=32, shuffle=True, num_workers=10,**{\"pin_memory\":True})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n",
      "True True\n"
     ]
    }
   ],
   "source": [
    "for data,indx in data_loader:\n",
    "    i = i+1\n",
    "    print(data[0].is_pinned(),indx.is_pinned())\n",
    "    #将数据拷贝到GPU\n",
    "    data.to(torch.device(\"cuda\"),non_blocking=True)\n",
    "    indx.to(torch.device(\"cuda\"),non_blocking=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自动设备感知"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([1, 2, 3], device='cuda:0')\n"
     ]
    }
   ],
   "source": [
    "#创建默认的CPU设备\n",
    "device = torch.device(\"cpu\")\n",
    "#如果GPU设备可用，将默认热备改为GPU\n",
    "if(torch.cuda.is_available()):\n",
    "    device = torch.device(\"cuda\")\n",
    "#使用tensor方法，device关键字参数指定设备\n",
    "a = torch.tensor([1,2,3],device=device)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter containing:\n",
      "tensor([[0.1795]], device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([0.8931], device='cuda:0', requires_grad=True)\n"
     ]
    }
   ],
   "source": [
    "#将网络迁移到GPU\n",
    "import torch.nn\n",
    "class LinearRegression(torch.nn.Module):\n",
    "    def __init__(self):\n",
    "        super(LinearRegression,self).__init__()\n",
    "        #输入输出都是一维\n",
    "        self.linear = torch.nn.Linear(1,1)\n",
    "    def forward(self,x):\n",
    "        return self.linear(x)\n",
    "regression = LinearRegression().to(device=device)\n",
    "for para in regression.parameters():\n",
    "    print(para)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter containing:\n",
      "tensor([[0.5079]], device='cuda:0', requires_grad=True)\n",
      "Parameter containing:\n",
      "tensor([0.9685], device='cuda:0', requires_grad=True)\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
